<script setup>
import mock from '~mock/menu'
import Info from './info.vue'

import { useConfirm } from '@/hooks/useConfirm'

import { ref } from 'vue'

const tableData = ref(mock || [])

// 表单状态
const filterState = ref(true)
// 表单数据
const formData = {
  pagenum: 1,
  pagesize: 10,
  username: '',
  mobile: '',
  role_name: '',
  created_at: '',
  updated_at: '',

  created_at_temp: [], // el-date-picker组件保存是数组
  updated_at_temp: [] // el-date-picker组件保存是数组
}

// 角色编辑
const authEditRef = ref()
const onUserInfo = (row) => {
  // console.log(row)

  authEditRef.value.state = true

  if (row) {
    authEditRef.value.formData.user_id = row.id
    authEditRef.value.formData.username = row.uname
    // authEditRef.value.formData.password =  切记密码这玩意 不用回显 不安全 =》 你去新浪微博、qq等常用网站    不传-使用旧密码也就是不改，传-更新
    authEditRef.value.formData.mobile = row.mobile
    authEditRef.value.formData.avatar = row.avatar
  }
}

const onAuthEdit = (row) => {
  console.log(row)
  authEditRef.value.state = true

  // 回显
  if (row) {
    authEditRef.value.formData.auth_id = row.auth_id

    authEditRef.value.formData.auth_name = row.auth_name
    authEditRef.value.formData.type = row.type // 1-菜单， 2-按钮

    authEditRef.value.formData.path = row.path
    authEditRef.value.formData.component = row.component
    authEditRef.value.formData.keep_alive = row.keep_alive //1-缓存,0-不缓存

    authEditRef.value.formData.pid = row.auth_pid
  }
}

// 权限删除
const onDelete = () => {
  useConfirm()
}
</script>
<template>
  <Box>
    <!-- 逻辑层 -->
    <Info ref="authEditRef" />
    <!-- 逻辑层 -->

    <!-- 筛选 -->
    <template #filter v-if="filterState">
      <el-form :inline="true" :model="formData">
        <el-form-item label="菜单名称">
          <el-input placeholder="请输入菜单名称" v-model="formData.username" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select placeholder="菜单状态" style="width: 120px">
            <el-option
              v-for="item in [
                { label: '正常', value: 1 },
                { label: '停用', value: 0 }
              ]"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"> <span class="iconfont icon-sousuo"></span>搜索 </el-button>
          <el-button type="default"> 重置 </el-button>
        </el-form-item>
      </el-form>
    </template>
    <!-- 筛选 -->

    <!-- 按钮组 -->
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="onUserInfo">
          <span class="iconfont icon-jiahao"></span>添加
        </el-button>
      </el-col>

      <el-col :span="12" style="text-align: right">
        <el-button circle @click="filterState = !filterState">
          <span class="iconfont icon-sousuo"></span>
        </el-button>
        <el-button circle>
          <span class="iconfont icon-shuaxin"></span>
        </el-button>
      </el-col>
    </el-row>
    <!-- 按钮组 -->

    <el-table :data="tableData" :border="true" class="qfTable" row-key="auth_id" default-expand-all>
      <el-table-column
        label="菜单名称"
        prop="auth_name"
        align="left"
        width="140"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="图标"
        prop="auth_name"
        align="center"
        width="140"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="排序"
        prop="auth_name"
        align="center"
        width="140"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column label="种类" align="center" width="200">
        <template #default="scoped">
          <el-tag v-if="scoped.row.type === 1">菜单</el-tag>
          <el-tag type="success" v-else>按钮</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="路由地址" prop="path" align="center"></el-table-column>
      <!-- <el-table-column label="组件路径" align="center" width="300">
        <template #default="scoped">
          <el-tag type="info" v-if="scoped.row.component">{{ scoped.row.component }}</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column label="状态" align="center" width="200">
        <template #default="scoped">
          <el-switch
            v-model="scoped.row.keep_alive"
            inline-prompt
            style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
            active-text="正常"
            inactive-text="禁用"
            :active-value="1"
            :inactive-value="0"
            disabled
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template #default="scoped">
          <el-button type="primary" link size="small" @click="onAuthEdit(scoped.row)">
            <span class="iconfont icon-bianji"></span>修改
          </el-button>
          <el-button type="primary" link size="small" @click="onDelete">
            <span class="iconfont icon-shanchu"></span>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </Box>
</template>

<style lang="scss" scoped></style>
